package com.pcitc.paas.feign.demo.consumer.server.controller;

import com.pcitc.paas.feign.demo.api.model.Person;
import com.pcitc.paas.feign.demo.consumer.server.client.ProviderClient;
import feign.Response;
import lombok.SneakyThrows;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * description: <br>
 * createTime: 2021/6/21 17:58 <br>
 *
 * @author zzh
 */
@Controller
@RequestMapping({"/consumer"})
public class ConsumerController {
    @Resource
    protected HttpServletRequest request;
    @Resource
    protected HttpServletResponse response;
    @Resource
    protected HttpSession session;
    @Resource
    ProviderClient providerClient;

    public ConsumerController() {
    }

    @RequestMapping({"/test1"})
    @ResponseBody
    public String test1() {
        return this.providerClient.test1("null", true, new Person("zhangsan", 23, "zs"));
    }

    @RequestMapping({"/test2"})
    @ResponseBody
    public String test2() {
        return this.providerClient.test2("null", true, new Person("zhangsan", 23, "zs"));
    }

    @RequestMapping({"/test3"})
    @ResponseBody
    public String test3() {
        return this.providerClient.testPathVariable("asdf", "zhangsan");
    }

    @RequestMapping({"/test4"})
    @ResponseBody
    public String test4() {
        return this.providerClient.testRequestBody(new Person("zhangsan", 23, "zs"));
    }

    @SneakyThrows
    @RequestMapping({"/upload1"})
    @ResponseBody
    public String upload1() {
        File file = new File("d:\\upload.txt");
        DiskFileItem fileItem = (DiskFileItem)(new DiskFileItemFactory()).createItem("file", "text/plain", true, file.getName());

        try (InputStream input = new FileInputStream(file);
             OutputStream os = fileItem.getOutputStream()) {
                IOUtils.copy(input, os);
        }
        MultipartFile multi = new CommonsMultipartFile(fileItem);
        return this.providerClient.testUpload1("test", multi);
    }

    @SneakyThrows
    @RequestMapping({"/upload2"})
    @ResponseBody
    public String upload2() {
        File file1 = new File("d:\\upload.txt");
        DiskFileItem fileItem1 = (DiskFileItem)(new DiskFileItemFactory()).createItem("file", "text/plain", true, file1.getName());

        try (InputStream input = new FileInputStream(file1);
             OutputStream os = fileItem1.getOutputStream()) {
                    IOUtils.copy(input, os);
        }

        MultipartFile multi1 = new CommonsMultipartFile(fileItem1);
        File file2 = new File("d:\\download.txt");
        DiskFileItem fileItem2 = (DiskFileItem)(new DiskFileItemFactory()).createItem("file", "text/plain", true, file2.getName());

        try (InputStream input = new FileInputStream(file2);
             OutputStream os = fileItem2.getOutputStream();) {
            IOUtils.copy(input, os);
        }
        MultipartFile multi2 = new CommonsMultipartFile(fileItem2);
        return this.providerClient.testUpload2("test", new MultipartFile[]{multi1, multi2});
    }

    @RequestMapping({"/upload3"})
    @ResponseBody
    public String upload3(MultipartFile file) {
        return this.providerClient.testUpload1("test", file);
    }

    @RequestMapping({"/upload4"})
    @ResponseBody
    public String upload4(MultipartFile[] file) {
        return this.providerClient.testUpload2("test", file);
    }

    @RequestMapping({"/download"})
    @ResponseBody
    public String download() {
        ResponseEntity<byte[]> responseEntity = this.providerClient.testDownload1("null");
        System.out.println("===========================");
        System.out.println(new String((byte[])responseEntity.getBody()));
        System.out.println("===========================");
        return "OK";
    }

    @RequestMapping({"/download1"})
    @ResponseBody
    public String download1() {
        ResponseEntity<byte[]> responseEntity = this.providerClient.testDownload2("asdf");
        System.out.println("===========================");
        System.out.println(new String((byte[])responseEntity.getBody()));
        System.out.println("===========================");
        return "OK";
    }

    @SneakyThrows
    @RequestMapping({"/download2"})
    @ResponseBody
    public String download2() {
        Response response = this.providerClient.testDownload3("asdf");

        try (InputStream inputStream = response.body().asInputStream();) {
            System.out.println("===========================");
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            System.out.println(new String(bytes));
            System.out.println(response.headers().get("Content-Disposition"));
            System.out.println("===========================");
        }

        return "OK";
    }
}
